using System;
using System.Collections.Generic;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Wicresoft.SMS.Send
{
    class Initialization
    {
        Office.CommandBar newToolBar;
        Office.CommandBarButton btnSetting;
        Office.CommandBarButton btnSend;
        Outlook.Explorers selectExplorers;

        Outlook._Application app;
        ThisAddIn tai;

        private global::System.Object missing = global::System.Type.Missing;


        private Initialization()
        { }

        private static Initialization ini;

        public static Initialization Instance
        {
            get
            {
                if (ini == null)
                    ini = new Initialization();
                return ini;
            }
        }

        public void Start(Outlook._Application _app, ThisAddIn _tai, string url)
        {
            try
            {
                app = _app;
                tai = _tai;

                if (ConfigManage.ProviderUrl == "")
                    ConfigManage.ProviderUrl = url;

                Logger.Info("Online, " + SysInfo);

                selectExplorers = app.Explorers;
                selectExplorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
                AddToolbar();

                ContactsGetter.Contacts = new GetContactsFromOutlook(this.GetContacts);
            }
            catch (Exception ex)
            {
                Logger.Exception("Initialization.Start", ex);                
            }
            
        }

        public void ShutDown()
        {
            Logger.Info("Offline, " + SysInfo);
        }


        private string SysInfo
        {
            get
            {
                try
                {

                    string sysinfo = "Outlook Version: " + app.Version + ", ";
                    sysinfo += "Outlook Language: " + app.LanguageSettings.get_LanguageID(Office.MsoAppLanguageID.msoLanguageIDUI) + ", ";
                    sysinfo += "User Locale: " + System.Windows.Forms.Application.CurrentCulture.DisplayName + ", ";
                    // sysinfo += "System Locale: " + Thread.CurrentThread.CurrentCulture.DisplayName + ", ";
                    sysinfo += Environment.OSVersion.VersionString + ",";
                    sysinfo += "Current WinUser: " + Environment.UserName + ", ";
                    sysinfo += "Current Domain: " + Environment.UserDomainName;
                    return sysinfo;
                }
                catch (Exception ex)
                {
                    Logger.Exception("Initialization.SysInfo", ex);
                    return "An exception occurred when getting SysInfo";
                }
            }
        }

        private void newExplorer_Event(Outlook.Explorer new_Explorer)
        {
            try
            {
                ((Outlook._Explorer)new_Explorer).Activate();
                newToolBar = null;
                AddToolbar();
            }
            catch (Exception ex)
            {
                Logger.Exception("Initialization.newExplorer_Event", ex);
            }

        }

        private void AddToolbar()
        {
            try
            {
                if (newToolBar == null)
                {
                    Office.CommandBars cmdBars = app.ActiveExplorer().CommandBars;
                    newToolBar = cmdBars.Add("SMS Sender", Office.MsoBarPosition.msoBarTop, false, true);
                    newToolBar.Visible = true;
                }

                Office.CommandBarButton smssetting = (Office.CommandBarButton)newToolBar.Controls.Add(1, missing, missing, missing, missing);
                smssetting.Caption = "SMS Setting";
                smssetting.Tag = "SMS Setting";
                if (this.btnSetting == null)
                {
                    this.btnSetting = smssetting;
                    btnSetting.Click += new Office._CommandBarButtonEvents_ClickEventHandler(Setting_ButtonClick);
                }

                Office.CommandBarButton send = (Office.CommandBarButton)newToolBar.Controls.Add(1, missing, missing, missing, missing);
                send.Caption = "SMS Send";
                send.Tag = "SMS Send";
                if (this.btnSend == null)
                {
                    this.btnSend = send;
                    btnSend.Click += new Office._CommandBarButtonEvents_ClickEventHandler(Send_ButtonClick);
                }

            }
            catch (Exception ex)
            {
                Logger.Exception("Initialization.AddToolbar", ex);
            }
        }

        private void Setting_ButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
        {
            try
            {
                Setting frmsetting = new Setting();
                frmsetting.Show();
            }
            catch (Exception ex)
            {
                Logger.Exception("Initialization.Setting_ButtonClick", ex);
            }
        }

        private void Send_ButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
        {
            try
            {
                //GetContacts();
                Send frmsend = new Send();
                frmsend.Show();
            }
            catch (Exception ex)
            {
                Logger.Exception("Initialization.Send_ButtonClick", ex);
            }

        }

        private Outlook.ContactItem[] GetContacts()
        {
            try
            {
                Outlook.MAPIFolder contactFolder = app.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                Outlook.Items items = contactFolder.Items;
                List<Outlook.ContactItem> list = new List<Outlook.ContactItem>();
                if (items != null)
                {                   
                    foreach (object var in items)
                    {
                        Outlook.ContactItem item = (Outlook.ContactItem)var;
                        if (!string.IsNullOrEmpty(item.MobileTelephoneNumber))
                            list.Add(item);
                    }
                }
                return list.ToArray();
            }
            catch (Exception ex)
            {
                Logger.Exception("Initialization.GetContacts ", ex);
                return null;
            }
           
        }
    }
}
